# 安装 pandas
# pip install pandas

import pandas as pd

# 读取 CSV 文件
state_abbrevs = pd.read_csv('state-abbrevs.csv')
state_areas = pd.read_csv('state-areas.csv')
state_population = pd.read_csv('state-population.csv')

# 打印数据的前几行以确认读取成功
print("state-abbrevs.csv 数据预览：")
print(state_abbrevs.head())

print("\nstate-areas.csv 数据预览：")
print(state_areas.head())

print("\nstate-population.csv 数据预览：")
print(state_population.head())
# 合并数据
merged_data = pd.merge(state_areas, state_population, left_on='state_name', right_on='STATE')

# 查看合并后的数据
print("合并后的数据预览：")
print(merged_data.head())
# 查看缺失数据
print("查看缺失数据：")
print(merged_data.isnull().sum())

# 设置索引
merged_data.set_index('state_name', inplace=True)

# 排序
sorted_data = merged_data.sort_values(by='POPESTIMATE2019', ascending=False)

# 查看排序后的数据
print("\n排序后的数据预览：")
print(sorted_data.head())
# 安装 plotly
# pip install plotly

import plotly.express as px

# 重置索引以便绘图
sorted_data.reset_index(inplace=True)

# 绘制柱状图
fig = px.bar(sorted_data, x='state_name', y='POPESTIMATE2019', title='各州人口估计值（2019）',
             labels={'state_name': '州名', 'POPESTIMATE2019': '人口估计值'})

# 显示图形
fig.show()